package vn.ikame.android.boojtran.truyenbua.logic;

import java.io.File;
import java.util.ArrayList;

import vn.ikame.android.boojtran.truyenbua.ListItem;
import vn.ikame.android.boojtran.truyenbua.object.Item;
import vn.ikame.android.boojtran.truyenbua.utility.StorageUtils;

public class ImageManager {
	private static ImageManager magager = new ImageManager();
	private ArrayList<String> listPathCache = new ArrayList<String>();
	private ArrayList<Item> listItem = new ArrayList<Item>();
	private int curItem = 0;

	public static ImageManager get() {
		return magager;
	}

	public void init() {
		listPathCache = new ArrayList<String>();
	}

	public void setListItem(ArrayList<Item> list) {
		this.listItem = list;
	}

	public ArrayList<Item> getListItem() {
		return listItem;
	}

	public void viewPic(String path) {
		for (int i = 0; i < listPathCache.size(); i++) {
			if (listPathCache.get(i).equals(path)) {
				listPathCache.remove(i);
				break;
			}
		}
		listPathCache.add(path);
		return;
	}

	public void finishDownload(String path) {
		if (listPathCache.size() >= CConstant.MAX_CACHE_FILE) {
			clearAnImageCache(StorageUtils.getPathFileSaveFromUrl(
					ListItem.menu, listPathCache.get(0)));
			listPathCache.remove(0);
		}
		viewPic(path);
	}

	public Item getItem(int index) {
		return listItem.get(index);
	}

	public Item getCurItem() {
		return listItem.get(curItem);
	}

	public String getLinkImage(int index) {
		return listItem.get(index).getContent();
	}

	public String getCurLinkImage() {
		return listItem.get(curItem).getContent();
	}

	public String getNextLinkImage() {
		if (canNext()) {
			return listItem.get(curItem + 1).getContent();
		}
		return null;
	}

	public String getPrevLinkImage() {
		if (canPrev()) {
			return listItem.get(curItem - 1).getContent();
		}
		return null;
	}

	public boolean canNext() {
		return (curItem < (listItem.size() - 1));
	}

	public boolean canPrev() {
		return curItem > 0;
	}

	public boolean nextImage() {
		if (canNext()) {
			curItem++;
			return true;
		}
		return false;
	}

	public boolean gotoImage(int index) {
		// if (index >= 1 && index <= CConstant.numOfImg) {
		// curImage = index - 1;
		// return true;
		// }
		return false;
	}

	public boolean prevImage() {
		if (canPrev()) {
			curItem--;
			return true;
		}
		return false;
	}

	public void setCurItem(int index) {
		curItem = index;
	}

	public int getCurItemIndex() {
		return curItem;
	}

	public void clearAnImageCache(String imagePath) {
		File filePath = new File(imagePath);
		System.out.println("Clear 1: " + imagePath);
		if ((filePath.exists()) && (filePath.canWrite())) {
			System.out.println("Clear 2: " + imagePath);
			filePath.delete();
		}
	}

	public void clearImageCache() {
		for (int i = 0; i < listPathCache.size(); i++) {
			clearAnImageCache(listPathCache.get(i));
		}
		listPathCache.removeAll(listPathCache);
	}
}
